package tgc.edu.ljc.layui.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.layui.custom.DataGridUtils;
import tgc.edu.ljc.layui.custom.LayUiResult;
import tgc.edu.ljc.layui.entity.Course;
import tgc.edu.ljc.layui.entity.Grade;
import tgc.edu.ljc.layui.entity.Stu;
import tgc.edu.ljc.layui.entity.SysUser;
import tgc.edu.ljc.layui.entity.TbClass;
import tgc.edu.ljc.layui.security.UserUtils;
import tgc.edu.ljc.layui.service.ClassService;
import tgc.edu.ljc.layui.service.CourseService;
import tgc.edu.ljc.layui.service.GradeService;
import tgc.edu.ljc.layui.service.StuService;

@Controller
@RequestMapping(value="/grade")
public class GradeController {
	@Autowired
	private GradeService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StuService stuService;
	@Autowired
	private UserUtils userUtils;
	
	@RequestMapping(value="/classGradeManage")
	public String classGradeManage(ModelMap map) {
		List<TbClass> bjs = classService.findAll();
		List<Course> courses = courseService.findAll();
		map.put("bjs", bjs);
		map.put("courses", courses);
		return "grade/classGradeManage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(Integer classId,Integer courseId) {
		PageRequest pageable=PageRequest.of(0, 1000);
		Page<Grade> page = service.findByIdIsNull(pageable);
		if(classId!=null&&courseId!=null) {
			page=service.findPageByClassIdAndCourseId(classId,courseId,pageable);
		}
		return DataGridUtils.buildResult(page);
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public Object update(Integer id,Integer grade) {
		Grade model=service.findById(id);
		model.setGrade(grade);
		service.save(model);
		return LayUiResult.getResult();
	}
	
	@RequestMapping(value="/peasonGradeManage")
	public String peasonGradeManage(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		map.put("bjs",classes);
		return "grade/peasonGradeManage";
	}
	
	@RequestMapping(value="/findPageByStuId")
	@ResponseBody
	public Object findPageByStuId(Integer stuId) {
		PageRequest pageable=PageRequest.of(0, 1000);
		Page<Grade> page = service.findByIdIsNull(pageable);
		if(stuId!=null) {
			page= service.findbyStuId(stuId,pageable);
		}
		return DataGridUtils.buildResult(page);
	}
	
	@RequestMapping(value="/allGradeList")
	public String allGradeList(ModelMap map) {
		List<TbClass> bjs = classService.findAll();
		map.put("bjs", bjs);
		return "grade/allGradeList";
	}
	
	@RequestMapping(value="/allClassGrade")
	public String allGradeByClassId(Integer classId,ModelMap map) {
		List<Course> courses=service.findCoursesByClassId(classId);
		List<List<Object>> cjs=new ArrayList<>();
		List<Stu> stus=stuService.findByTbClassId(classId);
		for (Stu stu : stus) {
			List<Object> list=new ArrayList<>();
			list.add(stu.getUsername());
			list.add(stu.getName());
			for(Course course:courses) {
				Grade grade=service.findByStuIdAndCourseId(stu.getId(), course.getId());
				list.add(grade==null?null:grade.getGrade());
			}
			cjs.add(list);
		}
		map.put("cjs", cjs);
		map.put("courses", courses);
		return "grade/allClassGrade";
	}
	
	@RequestMapping(value="/peasonGradeList")
	public String peasonGradeList(ModelMap map) {
		SysUser user = userUtils.getUser();
		List<List<Object>> grades=service.findbyStuId(user.getId());
		map.put("grades", grades);
		return "grade/peasonGradeList";
	}
	
	@RequestMapping(value="/classGradeList")
	public String classGradeList(ModelMap map) {
		SysUser user = userUtils.getUser();
		Stu stu=(Stu) user;
		Integer classId = stu.getTbClass().getId();
		return allGradeByClassId(classId,map);
	}
}
